/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "PriceIndicatorItem.h"
#include "DiagramView.h"

namespace QSThink
{
	PriceIndicatorItem::PriceIndicatorItem( DiagramView *view)
		: _view(view),_lowest(0),_highest(0)
	{
		setFlag(ItemIsSelectable);//ItemIsMovable
		setFlag(ItemSendsGeometryChanges);
		setCacheMode(DeviceCoordinateCache);
		setAcceptHoverEvents(false);

		setZValue(4);
	}

	

	QRectF PriceIndicatorItem::boundingRect() const
	{
		int w=_view->maxWidth();
		int h=_view->maxHeight();

		
		return QRectF(0,0,w, h);

	}


	QPainterPath PriceIndicatorItem::shape() const
	{
		QPainterPath path;
		path.addRect(boundingRect());
		return path;
	}

	void PriceIndicatorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		int w=_view->maxWidth();

		int y0=_view->getHighestPriceYPos();
		int y1=_view->getLowestPriceYPos();

		float priceHigh=_highest/100.0f;
		float priceLow=_lowest/100.0f;


		float priceStep=(priceHigh-priceLow)/STEP;
		float yStep=(y1-y0)/STEP;

		//draw highest line
		painter->setPen(Qt::black);
		painter->drawLine(0,y0,w,y0);



		QString valueStr=QString::number(priceHigh,'g',16);
		int width=valueStr.length()*6+4;
		painter->drawText(w-width,y0-2,valueStr);

		//draw middle line
		for (int i=1;i<STEP;i++){
			painter->drawLine(0,y0+i*yStep,w,y0+i*yStep);
			valueStr=QString::number(priceHigh-priceStep*i,'g',16);
			width=valueStr.length()*6+4;
			painter->drawText(w-width,y0+i*yStep-2,valueStr);

		}


		//draw lowest line
		painter->drawLine(0,y1,w,y1);
		valueStr=QString::number(priceLow,'g',16);
		width=valueStr.length()*6+4;
		painter->drawText(w-width,y1-2,valueStr);
		
	}

	QVariant PriceIndicatorItem::itemChange( GraphicsItemChange change, const QVariant &value )
	{
		
		return QGraphicsItem::itemChange(change, value);

	}

	void PriceIndicatorItem::mousePressEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mousePressEvent(evt);

	}

	void PriceIndicatorItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mouseReleaseEvent(evt);

	}

	

	PriceIndicatorItem::~PriceIndicatorItem()
	{
		
	}

	void PriceIndicatorItem::setLowestValue( double price )
	{
		_lowest=price;
		update();

	}

	void PriceIndicatorItem::setHighestValue( double price )
	{
		_highest=price;
		update();

	}

}
